#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int get_sp_count(const char *str);
static int GetField(char *epczBuf,char *opczStr, char cDelim, int eiFiledNum);
char * kill_lf( char *pczStr );
int urlencode_to_xml(char *source);
void proc_info(char *eopBuf,int str_len);

int url_validate_and_to_xml(char *source)
{
	int iRet = -1; 
	iRet = urlencode_to_xml(source);		
	return iRet;
}

int urlencode_to_xml(char *source)
{
	int iRet;
	int i;
	int num = get_sp_count(source);
	char tmp[1024*10];
	char name[100];
	char xml_data[1024*80];
	memset(xml_data,0x00,sizeof(xml_data));
	iRet = sprintf(xml_data,"%s","<?xml version=\"1.0\" encoding=\"UTF-8\"?><formdata_to_xml>");
	for(i = 0; i < num; i++)
        {
		memset(tmp,0x00,sizeof(tmp));
		memset(name,0x00,sizeof(name));
                GetField(source, tmp, '&', i+1);
		GetField(tmp,name, '=',1);	
		kill_lf(tmp);
		proc_info(tmp,sizeof(tmp));
		iRet += snprintf(xml_data+iRet,sizeof(xml_data)-iRet,"<%s>%s</%s>",name,tmp+strlen(name)+1,name);
		
        }
	strcat(xml_data,"</formdata_to_xml>");	
	printf("xml=[%s]",xml_data);
	return iRet;
}
/* 计算分割之后的数据数目 */
int get_sp_count(const char *str)
{
	int iRet = 0;
	while(*str++ != '\0')
	{
		if(*str == 0x26)
		{
			iRet++;
		}
	}
	//比分隔符多一个 
	return iRet+1;
}

static int GetField(char *epczBuf,char *opczStr, char cDelim, int eiFiledNum)
{
    int iNum=1;
    int iCout=0;
    int iFlag = 0;
    unsigned char ucTemp = 0;
    if ( !epczBuf || eiFiledNum <=0 )
        return -1;
    while ( *epczBuf != 0x00 )
    {
        ucTemp = (unsigned char)*epczBuf;

        if ( *epczBuf != cDelim || (*epczBuf == cDelim && iFlag))
        {
            if ( iNum == eiFiledNum )
            {
                opczStr[iCout] = *epczBuf;
                iCout++;
            }
        }
        else
        {
            iNum++;
            if ( iNum > eiFiledNum )
                break;
        }
        if(!iFlag && ucTemp > 127)
        {
            iFlag = 1;
        }
        else
        {
            iFlag = 0;
        }
        epczBuf++;
    }
    opczStr[iCout] = 0x00 ;
    if ( iNum < eiFiledNum )
        return -1;
    return iCout ;
}



char * kill_lf( char *pczStr )
{
    char *pczTmp ;
    pczTmp=pczStr;
    while( *pczTmp == 0x20 ) pczTmp++;
    pczStr = pczTmp;
    if(pczStr[0])
    {
        pczTmp=pczStr+strlen(pczStr) - 1;
        while ( *pczTmp==0x20 || *pczTmp==0x0d || *pczTmp==0x0a ) pczTmp--;
        *(pczTmp+1)=0x00;
    }
    return pczStr;
}


void proc_info(char *eopBuf,int str_len)
{
    char aczBuf[1024*10];
    char aczTemp[100];
    int j = 0;
    char *p = eopBuf;
    memset(aczBuf, 0x00, sizeof(aczBuf));

    if(!strstr(eopBuf, "%"))
    {
        return;
    }

    for(; *eopBuf; )
    {
        if(*eopBuf == '%')
        {
            memset(aczTemp, 0x00, sizeof(aczTemp));
            strncpy(aczTemp, eopBuf+1, 2);
            aczBuf[j++]= (char)strtol(aczTemp, NULL, 16);
            eopBuf+=3;
        }
        else
        {
            aczBuf[j++]=*eopBuf++;
        }
    }

    strncpy(p, aczBuf,str_len);
}
